import { createApp } from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import { i18n } from "@/i18n";
import "element-plus/dist/index.css";
import { Debounce } from "@/utils/debounce"; //防抖
require("@/timeabout");

const app = createApp(App).use(store).use(router);
app.use(i18n);
app.mount("#app");
app.config.globalProperties.debounce = Debounce; //防抖全局注册
//全局自定义指令
app.directive("btn", {
  mounted(el, binding) {
    let buttonKey = binding.value;
    // 代表某个元素需要通过权限验证
    if (buttonKey) {
      let key = checkKey(buttonKey);
      if (!key) {
        //没有权限
        el.remove(); //删除按钮
      }
    } else {
      throw new Error("缺少唯一指令");
    }
  },
});
// 检测传入的元素key是否可以显示
function checkKey(key) {
  let perms = store.state.admin.user.perms; //vuex中的权限数据（获取权限数组）
  let permissionData = perms ? perms : [];
  //如果传入的元素key不在权限数组里，则不可显示
  let index = permissionData.indexOf(key);
  if (index > -1) {
    return true;
  } else {
    return false;
  }
}
